package com.cskaoyan.service;

import com.cskaoyan.bean.bo.RefundBo;
import com.cskaoyan.bean.bo.ShipBo;
import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class MarketServiceImpl implements MarketService{

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;



    /**
    * @Description: /region
    * @Author: wxm
    * @Date: 2020/11/2
    */
    @Override
    public List<Region> getRegionList() {
        List<Region> regionList = regionMapper.selectByExample(new RegionExample());
        List<Region> regionListProvence = new ArrayList<>();
        for (Region region1:regionList){
            if (region1.getType()==1) {
                for (Region region2:regionList) {
                    if (region2.getType()==2){
                        for (Region region3: regionList){
                            if (region3.getType() == 3 && region3.getPid().equals(region2.getId())){
                                if (region2.getChildren() != null){
                                    region2.getChildren().add(region3);
                                }else {
                                    region2.setChildren(new LinkedHashSet<>());
                                    region2.getChildren().add(region3);
                                }
                            }
                        }
                        if (region2.getPid().equals(region1.getId())){
                            if (region1.getChildren()!=null){
                                region1.getChildren().add(region2);
                            }else {
                                region1.setChildren(new LinkedHashSet<>());
                                region1.getChildren().add(region2);
                            }
                        }
                    }
                }
                regionListProvence.add(region1);
            }
        }
        return regionListProvence;
    }


    @Override
    public Map<String,Object> getBrandList(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(sort+" "+order);
        BrandExample.Criteria criteria = brandExample.createCriteria().andDeletedEqualTo(false);
        if (id != null){
            criteria.andIdEqualTo(id);
        }
        if (name != null){
            criteria.andNameLike("%"+name+"%");
        }
        List<Brand> brandList = brandMapper.selectByExample(brandExample);
        PageHelper.startPage(page,limit);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("items",brandList);
        return map;
    }


    @Override
    public Brand brandCreate(Brand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        Integer i = brandMapper.insertSelective(brand);
        return brand;
    }

    @Override
    public Integer brandUpdate(Brand brand) {
        Integer i = brandMapper.updateByPrimaryKey(brand);
        return i;
    }

    @Override
    public Integer brandDelete(Brand brand) {
        brand.setDeleted(true);
        Integer i = brandMapper.updateByPrimaryKeySelective(brand);
        return i;
    }


    @Override
    public List<Category> getCategoryList() {
        CategoryExample categoryexample = new CategoryExample();
        categoryexample.createCriteria().andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryexample);
        List<Category> categoryListLv1= new ArrayList<>();
        for (Category category1: categoryList){
            if (category1.getLevel().equals("L1")){
                for (Category category2: categoryList){
                    if (category2.getLevel().equals("L2") && category2.getPid().equals(category1.getId())){
                        if (category1.getChildren() != null){
                            category1.getChildren().add(category2);
                        }else {
                            category1.setChildren(new LinkedHashSet<>());
                            category1.getChildren().add(category2);
                        }
                    }
                }
                categoryListLv1.add(category1);
            }
        }
        return categoryListLv1;
    }

    @Override
    public List<Map> getCategoryL1() {
        CategoryExample categoryExample = new CategoryExample();
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        List<Map> mapList = new ArrayList<>();
        for (Category category1:categoryList){
            if ("L1".equals(category1.getLevel())){
                Map<String,Object> map = new HashMap<>();
                map.put("value",category1.getId());
                map.put("label",category1.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public Category categoryCreate(Category category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        Integer i = categoryMapper.insertSelective(category);
        return category;
    }

    @Override
    public Integer categoryUpdate(Category category) {
        Integer i = categoryMapper.updateByPrimaryKey(category);
        return i;
    }

    @Override
    public Integer categoryDelete(Category category) {
        category.setDeleted(true);
        Integer i = categoryMapper.updateByPrimaryKeySelective(category);
        return i;
    }

    /**
    * @Description: /issue
    * @Author: wxm
    * @Date: 2020/11/2
    */

    @Override
    public Map<String, Object> getIssueList(Integer page, Integer limit, String sort, String order, String question) {
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort+" "+order);
        IssueExample.Criteria criteria = issueExample.createCriteria().andDeletedEqualTo(false);
        if (question != null){
            criteria.andQuestionLike("%" + question + "%");
        }
        List<Issue> issueList = issueMapper.selectByExample(issueExample);
        PageHelper.startPage(page,limit);
        PageInfo<Issue> pageInfo = new PageInfo<>(issueList);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("items",issueList);
        return map;
    }

    @Override
    public Issue issueCreate(Issue issue) {
        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());
        Integer i = issueMapper.insertSelective(issue);
        return issue;
    }

    @Override
    public Integer issueUpdate(Issue issue) {
        Integer i = issueMapper.updateByPrimaryKey(issue);
        return i;
    }

    @Override
    public Integer issueDelete(Issue issue) {
        issue.setDeleted(true);
        Integer i = issueMapper.updateByPrimaryKeySelective(issue);
        return i;
    }

    /**
    * @Description: /keyword
    * @Author: wxm
    * @Date: 2020/11/2
    */

    @Override
    public Map<String, Object> getKeywordList(Integer page, Integer limit, String sort, String order, String keyword, String url) {
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort+" "+order);
        KeywordExample.Criteria criteria = keywordExample.createCriteria().andDeletedEqualTo(false);
        if (keyword != null){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (url != null){
            criteria.andUrlLike("%"+url+"%");
        }
        List<Keyword> keywordList = keywordMapper.selectByExample(keywordExample);
        PageHelper.startPage(page,limit);
        PageInfo<Keyword> pageInfo = new PageInfo<>(keywordList);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("items",keywordList);
        return map;
    }

    @Override
    public Keyword keywordCreate(Keyword keyword) {
        keyword.setAddTime(new Date());
        keyword.setUpdateTime(new Date());
        keywordMapper.insertSelective(keyword);
        return keyword;
    }

    @Override
    public Integer keywordUpdate(Keyword keyword) {
        Integer i = keywordMapper.updateByPrimaryKey(keyword);
        return i;
    }

    @Override
    public Integer keywordDelete(Keyword keyword) {
        keyword.setDeleted(true);
        Integer i = keywordMapper.updateByPrimaryKeySelective(keyword);
        return i;
    }



    @Override
    public Map<String, Object> getOrderList(Integer page, Integer limit, String sort, String order,
                                            Integer userId, Integer orderSn, Short[] orderStatusArray) {
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(sort+" "+order);
        OrderExample.Criteria criteria = orderExample.createCriteria().andDeletedEqualTo(false);
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null){
            criteria.andOrderSnLike("%"+orderSn+"%");
        }
        if (orderStatusArray != null) {
            List<Short> resultList = new ArrayList<>(orderStatusArray.length);
            Collections.addAll(resultList, orderStatusArray);
            criteria.andOrderStatusIn(resultList);
        }
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        PageHelper.startPage(page, limit);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("items", orderList);
        return map;

    }

    @Override
    public Map<String,Object> orderDetail(Integer id) {
        Map<String,Object> map = new HashMap<>();
        Order order = orderMapper.selectByPrimaryKey(id);
        order.setEndTime(new Date());
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("nickname", user.getNickname());
        userMap.put("avatar", user.getAvatar());

        Map<String, Object> newMap = createNewMap(order);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(order.getId());
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
        map.put("orderGoods", orderGoodsList);
        map.put("user", userMap);
        map.put("order", newMap);
        return map;

    }

    private Map<String, Object> createNewMap(Order order) {
        order.setEndTime(new Date());
        HashMap<String, Object> orderMap= new HashMap<>();
        orderMap.put("id",order.getId());
        orderMap.put("userId",order.getUserId());
        orderMap.put("orderSn",order.getOrderSn());
        orderMap.put("orderStatus",order.getOrderStatus());
        orderMap.put("consignee",order.getConsignee());
        orderMap.put("mobile",order.getMobile());
        orderMap.put("address",order.getAddress());
        orderMap.put("message",order.getMessage());
        orderMap.put("goodsPrice",order.getGoodsPrice());
        orderMap.put("grouponPrice",order.getGrouponPrice());
        orderMap.put("freightPrice",order.getFreightPrice());
        orderMap.put("couponPrice",order.getCouponPrice());
        orderMap.put("integralPrice",order.getIntegralPrice());
        orderMap.put("orderPrice",order.getOrderPrice());
        orderMap.put("actualPrice",order.getActualPrice());
        orderMap.put("comments",order.getComments());
        orderMap.put("endTime",order.getEndTime());
        orderMap.put("addTime",order.getAddTime());
        orderMap.put("updateTime",order.getUpdateTime());
        orderMap.put("deleted",order.getDeleted());
        orderMap.put("confirmTime",order.getConfirmTime());
        orderMap.put("shipSn",order.getShipSn());
        orderMap.put("shipChannel",order.getShipChannel());
        orderMap.put("shipTime",order.getShipTime());
        return orderMap;
    }

    @Override
    public int ship(ShipBo orderShipBo) {
        Order order = new Order();
        order.setId(orderShipBo.getOrderId());
        order.setShipChannel(orderShipBo.getShipChannel());
        order.setShipSn(orderShipBo.getShipSn());
        order.setShipTime(new Date());
        order.setOrderStatus( 301);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int refund(RefundBo refundBo) {
        Order order = orderMapper.selectByPrimaryKey(refundBo.getOrderId());
        order.setOrderStatus( 203);
        order.setUpdateTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(refundBo.getOrderId());
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
        for (OrderGoods orderGoods : orderGoodsList){
            orderGoods.setUpdateTime(new Date());
            orderGoods.setDeleted(true);
        }
        return 1;
    }
}
